<?
session_start();
//This function securing the posted variable
function S($pValue)
{
	//echo 'posted value '.$pValue;
	$checked = str_ireplace("script", 'zzz',$pValue);
	$checked = str_ireplace('iframe', 'zzz',$pValue);
	
	return htmlspecialchars($checked);
	//return $checked;
}
//Check Null Boolean
function CN_B($pValue)
{
if($pValue == '' or $pValue == 'undefined')
{
	$pValue = '0';
}
return $pValue;
}
//Check Null String/Varchar
function CN_S($pValue)
{
	
if($pValue == "")
{
	$checked= "''";
}
elseif($pValue == "NOW()")
{
	$checked = $pValue;
}
else
{
	//$pValue = "'".mysql_real_escape_string($pValue)."'";
	//This checking string for injetions workds (INSERT, UPDATE, DELTE, ALTER, DROP)
	$checked = str_ireplace('INSERT', 'INSERT.',$pValue);
	$checked = str_ireplace('UPDATE', 'UPDATE.',$pValue);
	$checked = str_ireplace('DELETE', 'DELETE.',$pValue);
	$checked = str_ireplace('ALTER', 'ALTER.',$pValue);
	$checked = str_ireplace('DROP', 'DROP.',$pValue);
	$checked = str_ireplace('<script>', '<!--',$pValue);
	$checked = str_ireplace('</script>', '-->',$pValue);
	$checked = str_ireplace('<iframe', '--',$pValue);
	$checked = "'".mysql_real_escape_string(trim($checked))."'";
}
return $checked;
}
//Check Null Number(Float)
function CN_N($pValue)
{
if($pValue == '')
{
	$pValue = 0.0;
}
else
{
	$pValue = "".$pValue."";
}
return $pValue;
}
//Check Null Number(Int)
function CN_I($pValue)
{
if($pValue == '')
{
	$pValue = 0;
}
else
{
	$pValue = "".$pValue."";
}
return $pValue;
}
//Check Password or make it
function CN_P($pValue)
{
if($pValue == '')
{
	$pValue = "''";
}
else
{
	$pValue = "'".md5($pValue)."'";
}
return $pValue;
}
//PROTECTING FROM UN AUTHORIZE ACCESS

function protect_alreadyLoggedIn()
{
	if(isset($_COOKIE['memberid']))
		header("Location: ".SITEURL."settings.php");
}

function protect_admin()
{
	if(!isset($_COOKIE['admin']))
	{
			header("Location: index.php");
	}
}


function getImagePath($pImageName)
{
	$img_src = substr($pImageName,0,3);
	$img_src = ($img_src == 'URL') ? substr($pImageName,4) : fullURL('productimages/thumb/'.$pImageName);
	
	return $img_src;
}

function fixLengthWords($pStr,$pLength)
{
		$length = $pLength; // The number of words you want
	
		$text = strip_tags($pStr);
		/*echo $text;
		exit;*/
		$words = explode(' ', $text); // Creates an array of words	
		$words = array_slice($words, 0, $length);
		$str = implode(' ', $words);
		
		$str .= (count($words) < $pLength) ? '' : '...';
		
		return $str;
}
//=======================EMAIL RENDERING ==============================

function renderSignupEmail($uName, $uid)
{
	$html = '<html>
			<body style="font-family:Verdana; font-size:14px;">
			Dear '.$uName.'<br />
				
				<p>Your registration has been completed successfully with us. You need to activate your account by click following link.</p>
				
	<a href="'.SITEURL.'?page=register.php&sub=activateMe&buddy=$uid&token='.session_regenerate_id().'" target="_blank">
	Activate My Account</a>
	<br />
	<br />

	
	Thanks<br />

	Team '.SITEURL.'
	</body>
	</html>';
	
	return $html;
}

function sendMail($pTo, $pSubject, $pMessage, $pFrom = SITEADMIN)
{
	$headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= "From: $pFrom\r\n";
	
	//echo $pMessage;
	mail($pTo, $pSubject, $pMessage, $headers);
}

function generatePassword($length=9, $strength=0) {
	$vowels = 'aeuy';
	$consonants = 'bdghjmnpqrstvz';
	if ($strength & 1) {
		$consonants .= 'BDGHJLMNPQRSTVWXZ';
	}
	if ($strength & 2) {
		$vowels .= "AEUY";
	}
	if ($strength & 4) {
		$consonants .= '23456789';
	}
	if ($strength & 8) {
		$consonants .= '@#$%';
	}
 
	$password = '';
	$alt = time() % 2;
	for ($i = 0; $i < $length; $i++) {
		if ($alt == 1) {
			$password .= $consonants[(rand() % strlen($consonants))];
			$alt = 0;
		} else {
			$password .= $vowels[(rand() % strlen($vowels))];
			$alt = 1;
		}
	}
	return $password;
}

function renderRecoveryPassword($pFlag, $pPassword)
{
	$html = '<html>
			<body style="font-family:Verdana; font-size:14px;">
			Hi<br />';
			
		if($pFlag){
			$html .= '<p>Upon your request your Password is reset. Here below is your new password:</p>
				<strong>Password : '.$pPassword.'</script><br />
				<a href="'.SITEURL.'login.php" target="_blank">Login</a>';
		}
		else
		{
			$html .= '<h3>Ooops!</h3>
					<p>Your email does not exist in our records. Please Signup yourself.</p>
					<a href="'.SITEURL.'register.php" target="_blank">Signup Now</a>';
		}

	$html .= '<br /><br />

	
	Thanks<br />

	Team '.SITEURL.'
	</body>
	</html>';
	
	return $html;
}

function renderNotification($pText)
{
	$html = '<div class="ui-widget">
			<div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;"> 
				<p><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>
				'.$pText.'
				</p>
			</div>
			</div>';
	return $html;
}
function renderCombo($arrItem,$pName,$pValue,$pDisplay, $pSelected = null)        //a array with id, and name index
{
	//print_r($arrItem);
    $cmb = '<select name="'.$pName.'" id="'.$pName.'" class="name">
			<option value ="">Select</option>';
    for($i=0; $i<count($arrItem); $i++)
    {
		$selected = ($arrItem[$i][$pValue] == $pSelected) ? 'selected="selected"' : '';
        $cmb .= '<option value="'.$arrItem[$i][$pValue].'" '.$selected.'>'.$arrItem[$i][$pDisplay].'</option>';
    }
   
    $cmb .= '</select>';
	
	return $cmb;

}

function renderCombo2($arrItem,$pName,$pValue,$pDisplay, $pSelected = null)        //a array with id, and name index
{
	//print_r($arrItem);
    $cmb = '<select name="'.$pName.'" id="'.$pName.'" class="name">
			<option value ="">Select</option>';
    for($i=0; $i<count($arrItem); $i++)
    {
		$selected = ($arrItem[$i] == $pSelected) ? 'selected="selected"' : '';
        $cmb .= '<option '.$selected.'>'.$arrItem[$i].'</option>';
    }
   
    $cmb .= '</select>';
	
	return $cmb;

}

function makePageTitle($page, $param = null)
{
	$title = null;
	switch($page)
	{
		case 'aboutus':
			$title = 'About Us';
		break;
		
		case 'register':
			$title = 'Registration';
		break;
		
		default:
			$title = 'Multi Vendor Mall';
		break;
	}
	
	return $title;
}

		
function makeKeywords($pWord)
{
	$arrWord = explode(' ',$pWord);
	//print_r($arrWord);
	foreach($arrWord as $word)
	{
		$link = '<a href="search_item.php?item='.$word.'">'.$word.'</a>';
		echo $link.' ';
	}
		
}


function v($pCap, $pString, $pType = 'String', $pString2 = null)
{
	if($pType == 'email')
	{
		if($pString == '' or !isValidEmail($pString))	
		{
			$_SESSION[v] .= $pCap.' cannot be Null and Must be Email and Must be matched<br />';	
		}
		elseif($pString2 != '' and !isMatched($pString, $pString2))	
		{
			$_SESSION[v] .= $pCap.' cannot be Null and Must be Email and Must be matched<br />';	
		}
	}
	elseif($pType == 'password')
	{
		if($pString == '' or !isMatched($pString, $pString2))	
		{
			$_SESSION[v] .= $pCap.' cannot be Null and Must be Matched<br />';	
		}
	}
	elseif($pType == 'number')
	{
		if($pString == '' or !isNumber($pString))	
		{
			$_SESSION[v] .= $pCap.' cannot be Null and Must be Integer/Number<br />';	
		}
	}
	elseif($pType == 'integer')
	{
		if($pString == '' or !isInteger($pString))	
		{
			$_SESSION[v] .= $pCap.' cannot be Null and Must be Integer/Number<br />';	
		}
	}
	else
	{
		if($pString == '')
		{
			$_SESSION[v] .= $pCap.' cannot be Null<br />';	
		}
	}
}



function isValidEmail($email){
      $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
     
      if (eregi($pattern, $email)){
         return true;
      }
      else {
         return false;
      }   
   }
	
function isMatched($p1, $p2)
{
	if(strcmp($p1, $p2) == 0)
		return true;
	else
		return false;
}

function isInteger($pInteger)
{
	if(ctype_digit($pInteger) == 1)
		return true;
	else
		return false;
}

function isNumber($pNumber)
{
	if(is_numeric($pNumber))
		return true;
	else
		return false;
}

/* ================ Query Makers ======================= */
function qmInsert($pTable, $pData)
{
	$sql = "";

	$sql = "INSERT INTO ".$pTable." (";
	
	foreach($pData as $field => $value):
		$sql .= trim($field).",";
	endforeach;
	
	$sql = substr($sql, 0, -1);			//CLIPPING THE LAST ,

	$sql .= ") VALUES (";
	
	foreach($pData as $field => $value):
		$sql .= $value . ",";
	endforeach;
	
	$sql = substr($sql, 0, -1);			//CLIPPING THE LAST ,
	
	$sql .= ")";
	

	return $sql;
}
/* ================ Query Makers ======================= */
?>